<?php
/**
 * Created by PhpStorm
 * User: Kong
 * Date: 2025/9/25
 * Time: 12:34
 */

namespace App\Service\Admin;
use App\Constants\ErrorCode;
use Hyperf\DbConnection\Db;
class RoleService
{
    /**
     * 创建角色.
     * @throws \Throwable
     * @return mixed
     */
    public static function createRole(array $data)
    {
        try {
            Db::beginTransaction();
            $role = \App\Model\Role::query()->create($data);
            if (isset($data['menus_id']) && count($data['menus_id']) > 0) {
                $rolesMenus = [];
                foreach (array_values($data['menus_id']) as $menu_id) {
                    $rolesMenus[] = [
                        'role_id' => $role->id,
                        'menu_id' => $menu_id,
                    ];
                }
                \App\Model\RoleMenu::query()->insert($rolesMenus);
            }
            if (isset($data['permission_ids']) && count($data['permission_ids']) > 0) {
                $rolesPermissions = [];
                foreach (array_values($data['permission_ids']) as $permissions_id) {
                    $rolesPermissions[] = [
                        'role_id' => $role->id,
                        'permission_id' => $permissions_id,
                    ];
                }
                \App\Model\RolePermission::query()->insert($rolesPermissions);
            }
            Db::commit();
            return $role;
        } catch (\Exception $exception) {
            Db::rollBack();
            throw new \Exception($exception->getMessage(), $exception->getCode());
        }
    }
    /**
     * 更新角色以及角色菜单.
     * @throws \Throwable
     */
    public static function updateRole(array $data)
    {
        try {
            Db::beginTransaction();
            $id = $data['id']??0;
            if ($id<=0){
                throw new \Exception('角色ID错误', ErrorCode::PARAM_ERROR);
            }
            $role = \App\Model\Role::query()->find($id);
            if (! $role) {
                throw new \Exception('未找到角色数据', ErrorCode::DATA_NOT_EXIST);
            }
            $role->fill($data)->update();
            \App\Model\RoleMenu::query()->where('role_id', $role->id)->delete();
            if (isset($data['menus_id']) && count($data['menus_id']) > 0) {
                $rolesMenus = [];
                foreach ($data['menus_id'] as $menu_id) {
                    $rolesMenus[] = [
                        'role_id' => $role->id,
                        'menu_id' => $menu_id,
                    ];
                }
                \App\Model\RoleMenu::insert($rolesMenus);
            }
            \App\Model\RolePermission::query()->where('role_id', $role->id)->delete();
            if (isset($data['permission_ids']) && count($data['permission_ids']) > 0) {
                $rolesPermissions = [];
                foreach (array_values($data['permission_ids']) as $permissions_id) {
                    $rolesPermissions[] = [
                        'role_id' => $role->id,
                        'permission_id' => $permissions_id,
                    ];
                }
                \App\Model\RolePermission::insert($rolesPermissions);
            }
            Db::commit();
            $role->menus;
            $role->permissions;
            return $role;
        } catch (\Throwable $exception) {
            Db::rollBack();
            throw $exception;
        }
    }
}